package cn.jinjimi.app.custom.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.bean.Permissions;
import cn.jinjimi.app.controller.event.MakeCardExitEvent;
import cn.jinjimi.app.controller.event.MakeCardShareBtnEvent;
import cn.jinjimi.app.custom.ui.fragment.MakeCardFragment;
import cn.jinjimi.app.entity.CardListBean;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.EventBus;

/**
 * 贺卡的制作
 * 祝福语的编辑
 * 贺卡的生成
 * 分享贺卡
 * Created by Jinsen on 2015/5/14.
 */
public class MakeCardActivity extends ToolBarCommonActivity {

    private MakeCardFragment fragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Permissions.checkPermissions(this, Permissions.REQUEST_MEDIA_PERMISSIONS);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] results) {

        super.onRequestPermissionsResult(requestCode, permissions, results);
        Permissions.onRequestPermissionsResult(this, requestCode, permissions, results);
    }


    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_make_card_root_layout);
    }

    @Override
    protected void initial() {
        initView();
    }

    protected void initView() {
        int mType = getIntent().getIntExtra("type", Constants.REMIND_TYPE_BIRTHDAY);

        String type = getIntent().getStringExtra("type");
        try {
            int htype = Integer.valueOf(type);
            mType = htype > 0 ? htype : mType;
        } catch (Exception e) {
        }
        String mTitle = getIntent().getStringExtra("title");
        String mId = getIntent().getStringExtra("id");
        String mCardId = getIntent().getStringExtra("cardId");
        String mEnter = getIntent().getStringExtra("enter");

        CardListBean.CardBean bean = (CardListBean.CardBean) getIntent().getSerializableExtra("cardBean");
        setTitle(R.string.remind_default_title);
        setRightButton(R.drawable.nav_icon_share);

        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fragment = new MakeCardFragment();
        Bundle data = new Bundle();
        data.putInt("type", mType);
        data.putString("title", mTitle);
        data.putString("id", mId);
        data.putString("cardId", mCardId);
        data.putString("enter", mEnter);

        data.putSerializable("cardBean", bean);
        fragment.setArguments(data);
        ft.add(R.id.content_fragment, fragment, "MAKECARD");
        ft.commit();

        setSwipeEnabled(false);
    }

    @Override
    protected void rightAction() {
        super.rightAction();
        EventBus.getDefault().post(new MakeCardShareBtnEvent());
    }

    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new MakeCardExitEvent());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
